package com.meedoll.manager.std.server.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;

public class DeloyToServer {
	
	public static void deloy(String servicePath, File mvFile) {
		File desc = new File(servicePath + java.io.File.separator
				+ "meedoll.war");
		if (mvFile.exists())
			mvFile.renameTo(desc);
	}


	public static void compress(String srcPathName, String desc) {
		File srcdir = new File(srcPathName);
		if (!srcdir.exists())
			throw new RuntimeException(srcPathName + "不存在！");
		File zipFile = new File(desc);

		Project prj = new Project();
		Zip zip = new Zip();
		zip.setProject(prj);
		zip.setDestFile(zipFile);
		FileSet fileSet = new FileSet();
		fileSet.setProject(prj);
		fileSet.setDir(srcdir);
		// fileSet.setIncludes("**/*.java"); 包括哪些文件或文件夹
		// eg:zip.setIncludes("*.java");
		// fileSet.setExcludes(".svn"); //排除哪些文件或文件夹
		zip.addFileset(fileSet);

		zip.execute();

	}

	public static void runService(){
		Runtime runtime = Runtime.getRuntime();
		Process process = null;
		String s = null;
		try {
			 process = runtime.exec("D:\\javawork\\gwt-tomcat-6.0.18\\bin\\startup.bat");
			 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
			 while((s=bufferedReader.readLine()) != null)
				 System.out.println(s);
			 process.waitFor();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void printStack(OutputStream os){
		
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String path = System.getProperty("user.dir") + java.io.File.separator
				+ "war";
		String pathFile = path + java.io.File.separator + "meedoll.war";
		System.out.println(pathFile);
		compress(path, pathFile);
		String servier = "D:\\javawork\\gwt-tomcat-6.0.18\\webapps";
		File pf = new File(pathFile);
		while (true) {
			if (pf.exists()) {
				deloy(servier, new File(pathFile));
				break;
			}
		}
		
		//启动tomcat
		runService();
		System.out.println("succ");
	}

}
